package com.bw.car.page

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.EditText
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.car.R
import com.bw.car.adapter.AddCarAdapter
import com.bw.car.databinding.ActivityCarPayBinding
import com.bw.car.entity.AddCarEntity
import com.bw.car.vm.CarPayViewModel
import com.bw.common.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

@Route(path = RouterPath.PATH_CAR_PAY)
@AndroidEntryPoint
class CarPayActivity : BaseMvvmActivity<ActivityCarPayBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_car_pay

    private val mViewModel:CarPayViewModel by viewModels()

    val addAdapter = AddCarAdapter()
    var index:Int = 0

    var carCodeR:String = ""
    var carTimeR:String = ""


    override fun initViews() {
        super.initViews()
        val editText: Array<EditText> = arrayOf(
            mDataBinding.province,
            mDataBinding.et1,
            mDataBinding.et2,
            mDataBinding.et3,
            mDataBinding.et4,
            mDataBinding.et5,
            mDataBinding.et6,
            mDataBinding.et7)
        mDataBinding.rv.adapter = addAdapter
        mViewModel.getCarPayData()
        // 获取当前时间的 Calendar 实例
        val calendar = Calendar.getInstance()
        // 年份加1
        calendar.add(Calendar.YEAR, 1)
        // 格式化日期（注意格式中的“yyyy”要匹配 Calendar 的时间）
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        carTimeR = dateFormat.format(calendar.time)
        mDataBinding.carTime.text = carTimeR
        //点击省份添加到第一个输入框
        addAdapter.setOnItemClickListener { adapter, view, position ->
            editText[0].setText(addAdapter.data[position].abbreviation)
        }

        for (i in editText.indices){
            editText[i].addTextChangedListener(object :TextWatcher{
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                }

                override fun afterTextChanged(p0: Editable?) {
                    if (p0?.length==1){
                        if (index<editText.size-1){
                            index++
                            editText[index].requestFocus()
                        }
                        if (index == editText.size-1){
                            carCodeR = mDataBinding.province.text.toString()+mDataBinding.et1.text.toString()+mDataBinding.et2.text.toString()+mDataBinding.et3.text.toString()+mDataBinding.et4.text.toString()+mDataBinding.et5.text.toString()+mDataBinding.et6.text.toString()+mDataBinding.et7.text.toString()
                            mDataBinding.carCode.text = carCodeR
                        }
                    }else if (p0?.length ==0){
                        if (index>0){
                            editText[index-1].requestFocus()
                        }
                    }
                }

            })
        }
        //点击添加车辆
        mDataBinding.addCarPayment.setOnClickListener {
            //判断是否为空  为空的时候提示
            if (mDataBinding.carCode.text.isEmpty()){
                ToastUtils.showLong("请输入车牌号！")
                return@setOnClickListener
            }else{
                val carAddEntity = AddCarEntity(
                    id = 0,
                    licensePlate = mDataBinding.carCode.text.toString(),
                    ownerName = "张三",
                    provinceAbbreviation = mDataBinding.province.text.toString(),
                    userId = 1
                )
                mViewModel.getAddCarData(carAddEntity)
            }
        }








    }

    override fun loadData() {
        super.loadData()

        mViewModel.mCarPaySuccess.observe(this){
            addAdapter.data.addAll( it)
            addAdapter.notifyDataSetChanged()
        }

        //添加
        mViewModel.mAddCarSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            ARouter.getInstance().build(RouterPath.PATH_PAYMENT_PATH)
                .withString("carCode",carCodeR)
                .withString("carTime",carTimeR)
                .navigation()
        }
    }

    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mCarPaySuccess.observe(this){
            Log.d("--->sss","${it}")
        }
    }

}